package com.example.administrator.myproject.home

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast

import com.example.administrator.myproject.R
import com.example.administrator.myproject.utils.SPUtils

import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.QueryListener
import cn.bmob.v3.listener.SaveListener
import com.example.administrator.myproject.App
import com.example.administrator.myproject.bean.BobmBean
import com.example.administrator.myproject.bean.Collect

class Details : AppCompatActivity(), View.OnClickListener {
    private var name: TextView? = null
    private var money: TextView? = null
    private var coum: TextView? = null
    private var details: TextView? = null
    private var addCart: TextView? = null
    private var pay: TextView? = null
    private var recyclerView: RecyclerView? = null
    private var adapter: CommimtAdapter? = null
    private var imageView: ImageView? = null
    private var moeny: Int? = null
    private var type: String? = null
    private var flies: String? = null
    private fun getObject(objectId: String?) {
        var bmobQuery: BmobQuery<BobmBean> = BmobQuery()
        bmobQuery.getObject(objectId, object : QueryListener<BobmBean>() {
            override fun done(bobmBean: BobmBean, ex: BmobException?) {
                Log.i(Details.TAG, "done: "+bobmBean.toString())
                if (ex == null) {
                    name!!.text = bobmBean.name
                    if (bobmBean.name!!.equals("拌饭")) {
                        imageView!!.setImageDrawable(resources.getDrawable(R.drawable.laodiexie))
                    } else if (bobmBean.name!!.equals("米饭")) {
                        imageView!!.setImageDrawable(resources.getDrawable(R.drawable.refeng))
                    } else if (bobmBean.name!!.equals("米线")) {
                        imageView!!.setImageDrawable(resources.getDrawable(R.drawable.nanxie))
                    } else if (bobmBean.name!!.equals("面")) {
                        imageView!!.setImageDrawable(resources.getDrawable(R.drawable.kuzi_9_fen))
                    } else {
                        imageView!!.setImageDrawable(resources.getDrawable(R.drawable.mian_fan))
                    }
                    money!!.text = bobmBean.money.toString()
                    coum!!.text = "库存：" + bobmBean.coum!!
                    details!!.text = bobmBean.explain
                    type = bobmBean.type
                    moeny = bobmBean.money
                    Toast.makeText(this@Details, "查询成功", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(this@Details, ex.message, Toast.LENGTH_LONG).show()
                }
            }
        })
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.details_view1)
        imageView = findViewById(R.id.details_iv)
        recyclerView = findViewById(R.id.commimt_rv)
        name = findViewById(R.id.details_tv1)
        money = findViewById(R.id.details_tv2)
        coum = findViewById(R.id.details_tv3)
        details = findViewById(R.id.details_tv4)
        addCart = findViewById(R.id.details_tv5)
        pay = findViewById(R.id.details_tv6)
        recyclerView!!.layoutManager = LinearLayoutManager(this)
        adapter = CommimtAdapter(this)
        recyclerView!!.adapter = adapter
        val ObjectId = intent.getStringExtra("objectId")
        val bmobQuery = BmobQuery<BobmBean>()
        getObject(ObjectId)
        addCart!!.setOnClickListener(this)
        pay!!.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.details_tv5 -> {
                val addCart = AddCart()
                addCart.coum = 1
                addCart.moeny = moeny
                addCart.type = type
                addCart.name = name!!.text.toString()
                addCart.save(object : SaveListener<String>() {
                    override fun done(s: String, e: BmobException?) {
                        if (e == null) {
                            Toast.makeText(this@Details, "添加成功", Toast.LENGTH_SHORT).show()
                            finish()
                        } else {
                            Toast.makeText(this@Details, "添加失败", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            }
            R.id.details_tv6 -> {
                var application: App = this!!.application as App
                val s = SPUtils.get(this, "money", "") as String
                val i = application.getMoney()
                val i1 = moeny
                if (i!! < i1!!) {
                    Toast.makeText(this, "余额不足", Toast.LENGTH_SHORT).show()
                } else {
                    val i2 = i - i1
                    val collect = Collect()
                    collect.name = name!!.text.toString()
                    collect.money = money!!.text.toString()
                    collect.flie = flies
                    collect.coum = "1"
                    collect.save(object : SaveListener<String>() {
                        override fun done(s: String, e: BmobException?) {
                            if (e == null) {
                                SPUtils.put(this@Details, "money", i2.toString() + "")
                                Toast.makeText(this@Details, "支付成功", Toast.LENGTH_SHORT).show()
                                finish()
                            } else {
                                Toast.makeText(this@Details, "购买失败", Toast.LENGTH_SHORT).show()
                            }
                        }
                    })
                }
            }
        }
    }

    companion object {
        private val TAG = "Details"
    }
}
